<?php

namespace app\ljhs\controller;
use app\common\controller\Ljhs;
use app\admin\model\AdminLog;
use fast\Random;
use think\captcha\Captcha;
use think\Config;
use think\Hook;
use think\Validate;
/**
 * 用户接口
 */
class User extends Ljhs
{
    protected $noNeedLogin = ['init', 'captcha','test','login'];
    protected $noNeedRight = '*';

    public function test()
    {
        $this->success('ok');
    }

    public function getUserInfo()
    {
        $user = $this->auth->getUserInfo();
        //获取此用户属于哪个站点
        $user['stand'] = \app\common\model\Stand::where('id',$user['stand_id'])->find();

        $this->success('ok',$this->auth->getUserInfo());
    }

    /**
     * 登录[帐号和密码]
     * @ApiMethod (post)
     * @ApiParams(name="username", type="string", required=true, description="帐号")
     * @ApiParams(name="password", type="string", required=true, description="密码")
     * @ApiParams(name="keeplogin", type="string", required=false, description="保持登录，暂时未做")
     */
    public function login()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');

        $rule = [
            'username' => 'require|length:3,30',
            'password' => 'require|length:3,30',
        ];
        $data = [
            'username' => $username,
            'password' => $password,

        ];
        $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password')]);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        $result = $this->auth->login($username, $password);
        if ($result === true) {
            AdminLog::setTitle(__('移动端帐号登录'));
//            Hook::listen("admin_login_after", $this->request);
//            $this->success(__('Login successful'), $this->auth->getUserInfo());
            $admin = $this->auth->getAdmin();
            if ($admin) {
                Hook::listen("admin_login_after", $this->request);
                $this->success(__('Login successful'), $admin);
            }
        } else {
            $msg = $this->auth->getError();
            $msg = $msg ? $msg : __('Username or password is incorrect');
            $this->error($msg);
        }
    }


}
